<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true">
      <el-form-item label="园区" prop="area">
        <el-select v-model="queryParams.area" placeholder="请选择园区"  style="width: 240px" @change="handleQuery">
          <el-option label="全部" value="" ></el-option>
          <el-option v-for="item in dict.type.user_area" :label="item.label" :value="item.value" ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="食材名称" prop="name" v-show="showSearch">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入食材名称"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item v-show="showSearch">
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-form>
    <el-table  :data="stockList" >
<!--      <el-table-column type="selection" width="55" align="center" />-->
<!--      <el-table-column label="ID" prop="id" width="100" align="center"/>-->
      <el-table-column label="食材编号" prop="resourceId" width="120" align="center"/>
      <el-table-column label="园区" prop="area" width="120" align="center"/>
      <el-table-column label="食材名称" prop="name" min-width="120" align="center"/>
<!--      <el-table-column label="食材图片" prop="imageUrl" align="center" :width="imageColWidth">-->
<!--        <template v-slot:default="scope">-->
<!--          <el-image v-for="(item,index) in scope.row.images"-->
<!--            style="width: 100px; height: 100px; margin-right: 15px"-->
<!--            :src="baseUrl + item">-->
<!--          </el-image>-->
<!--        </template>-->
<!--      </el-table-column>-->
      <el-table-column label="实时库存" prop="amount" width="120" align="center">
      </el-table-column>
<!--      <el-table-column label="上期库存数量" prop="lastAmount" width="120" align="center">-->
<!--      </el-table-column>-->
      <el-table-column label="均价" prop="avgPrice" width="90" align="center">
        <template v-slot="{row}">
          <span>{{row.avgPrice | currency}}</span>
        </template>
      </el-table-column>
      <el-table-column label="单位" prop="unit" width="90" align="center"/>
      <el-table-column label="库存上限" prop="up" width="140" align="center">
        <template #default="scope">
          <span v-show="!scope.row.editFlag" >{{scope.row.up}}</span>
          <el-input-number v-show="scope.row.editFlag" v-model="scope.row.up" :precision="2" :min="0" :controls="false" style="width: 120px"></el-input-number>
        </template>
      </el-table-column>
      <el-table-column label="库存下限" prop="down" width="140" align="center">
        <template #default="scope">
          <span v-show="!scope.row.editFlag" >{{scope.row.down}}</span>
          <el-input-number v-show="scope.row.editFlag" v-model="scope.row.down" :precision="2" :min="0" :controls="false" style="width: 120px"></el-input-number>
        </template>
      </el-table-column>
      <el-table-column label="保质期" prop="guarantee" width="180" align="center">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.guarantee, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建人" prop="createBy" width="100" align="center"/>
      <el-table-column label="更新人" prop="updateBy" width="100" align="center"/>
      <el-table-column label="创建时间" align="center" prop="createTime" width="160">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="修改时间" align="center" prop="createTime" width="160">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="180" fixed="right">
        <template v-slot="{row}">
          <el-button size="small"
                     type="text"
                     icon="CircleCheck"
                     v-show="row.editFlag"
                     @click="submit(row)">保存
          </el-button>
          <el-button size="small"
                     type="text"
                     icon="CircleClose"
                     v-show="row.editFlag"
                     @click="cancel(row)">取消
          </el-button>
          <el-button v-show="!row.editFlag" size="mini" type="text" icon="el-icon-edit" @click="edit(row)">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleDetail(row)">明细</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList"
    />
  </div>
</template>

<script>
import {foodStoreList, updateStock} from "@/api/store/resource";
import {getDicts} from "@/api/system/dict/data";
import {overflowReportUpdate} from "@/api/store/calculate/calculate";

export default {
  name: "Stock",
  dicts:['user_area'],
  data() {
    return {
      baseUrl: process.env.VUE_APP_BASE_API,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      stockList:[],
      // 食材表格数据
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: '',
        area:','
      },
    };
  },
  created() {
    this.queryParams.area=this.$store.state.user.userInfo.area || ''
    this.getList();
    // this.getDictData()
  },
  methods: {
    /** 查询列表 */
    getList() {
      foodStoreList(this.queryParams).then(res => {
        res.rows.map(item=>{
          item.editFlag = false;
        })
        this.stockList=res.rows
        this.total = res.total;
      });
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },

    getDictData(){
      getDicts('food_unit').then(res=>{
        this.foodUnit=res.data
      })
    },
    handleDetail(row){
      this.$router.push({
        path:"/store/stock/detail/",
        query:{
          resourceId:row.resourceId,
          area:row.area
        }
      });
    },
    edit(row,index){
      row.editFlag = true;
      // this.$set(this.reportList, index, row);
    },
    submit(row){
      let updateData = {
        id: row.id,
        up: row.up,
        down:row.down,
        // area:row.area
      }
      updateStock(updateData).then(res => {
        this.$modal.msgSuccess("更新成功");
        row.editFlag = false;
        this.getList();
      })
    },
    cancel(row){
      row.editFlag = false;
      this.getList();
    }
  }
};
</script>
